const{ router,db_query}= require('./config')
// const DB = require('../../App/DB')
// const db_query = require('../../App/DB')
const token = require('../../App/token')

router.get('/login', async (ctx, next) => {
    //验证用户名密码是否为空
    // console.log(123);
    let sqls = 'select * from storetable  where phone="' + ctx.query.username + '" limit 1'

    let ress = await db_query(sqls)
    // console.log(ress,111)

    // console.log(ctx.query,9);
    if (ress=='' ) {
        ctx.body = { code: '404', codeinfo: '用户名或密码不存在' }
        // console.log('if');
    } else {
        let sql = 'select * from headtable where phone="' + ctx.query.username + '" limit 1'
        let res = await db_query(sql)
        // console.log(res, 15);
        //判断密码是否正确
        if (res[0].password != ctx.query.password) {
            ctx.body = { code: '500', codeinfo: '密码错误' }
        } else {
            // ctx.body = '1111'

            //此处应该进行token生成 然后将token返回给客户端
            let tokens = await token.getToken(res[0].id, 'headEnd')
            // console.log(tokens,23);
            
            ctx.body = { code: '200', codeinfo: '成功', token: tokens }
        }
        // console.log('else');
        // console.log(res);
    }
})

module.exports = router